home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gdevmem.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  10KB  |  248 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevmem.h */
  20. /* Private definitions for memory devices. */
  21.  
  22. /*
  23.    The representation for a "memory" device is simply a
  24.    contiguous bitmap stored in something like the PostScript
  25.    representation, i.e., each scan line (in left-to-right order), padded
  26.    to a 32-bit boundary, followed immediately by the next one.
  27.  
  28.    The representation of strings in the Ghostscript interpreter limits
  29.    the size of a string to 64K-1 bytes, which means we can't simply use
  30.    a string for the contents of a memory device.
  31.    We get around this problem by making the client read out the
  32.    contents of a memory device bitmap in pieces.
  33.  
  34.    On 80x86 PCs running in 16-bit mode, there may be no way to
  35.    obtain a contiguous block of storage larger than 64K bytes,
  36.    which typically isn't big enough for a full-screen bitmap.
  37.    We take the following compromise position: if the PC is running in
  38.    native mode (pseudo-segmenting), we limit the bitmap to 64K;
  39.    if the PC is running in protected mode (e.g., under MS Windows),
  40.    we assume that blocks larger than 64K have sequential segment numbers,
  41.    and that the client arranges things so that an individual scan line,
  42.    the scan line pointer table, and any single call on a drawing routine
  43.    do not cross a segment boundary.
  44.  
  45.    Even though the scan lines are stored contiguously, we store a table
  46.    of their base addresses, because indexing into it is faster than
  47.    the multiplication that would otherwise be needed.
  48. */
  49.  
  50. /* ------ Generic macros ------ */
  51.  
  52. /* Macro for declaring the essential device procedures. */
  53. #define declare_mem_map_procs(map_rgb_color, map_color_rgb)\
  54.   private dev_proc_map_rgb_color(map_rgb_color);\
  55.   private dev_proc_map_color_rgb(map_color_rgb)
  56. #define declare_mem_procs(copy_mono, copy_color, fill_rectangle)\
  57.   private dev_proc_copy_mono(copy_mono);\
  58.   private dev_proc_copy_color(copy_color);\
  59.   private dev_proc_fill_rectangle(fill_rectangle)
  60.  
  61. /* Macro for generating the procedure record in the device descriptor */
  62. extern dev_proc_open_device(mem_open);
  63. extern dev_proc_get_initial_matrix(mem_get_initial_matrix);
  64. extern dev_proc_close_device(mem_close);
  65. extern dev_proc_get_bits(mem_get_bits);
  66. extern dev_proc_get_xfont_procs(mem_get_xfont_procs);
  67. extern dev_proc_get_xfont_device(mem_get_xfont_device);
  68. #define mem_procs(map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle)\
  69. {    mem_open,\
  70.     mem_get_initial_matrix,\
  71.     gx_default_sync_output,\
  72.     gx_default_output_page,\
  73.     mem_close,\
  74.     map_rgb_color,            /* differs */\
  75.     map_color_rgb,            /* differs */\
  76.     fill_rectangle,            /* differs */\
  77.     gx_default_tile_rectangle,\
  78.     copy_mono,            /* differs */\
  79.     copy_color,            /* differs */\
  80.     gx_default_draw_line,\
  81.     mem_get_bits,\
  82.     gx_default_get_props,\
  83.     gx_default_put_props,\
  84.     gx_default_map_cmyk_color,\
  85.     mem_get_xfont_procs,\
  86.     mem_get_xfont_device\
  87. }
  88.  
  89. /*
  90.  * Macro for generating the device descriptor.
  91.  * Various compilers have problems with the obvious definition
  92.  * for max_value, namely:
  93.  *    (depth >= 8 ? 255 : (1 << depth) - 1)
  94.  * I tried changing (1 << depth) to (1 << (depth & 15)) to forestall bogus
  95.  * error messages about invalid shift counts, but the H-P compiler chokes
  96.  * on this.  Since the only values of depth we ever plan to support are
  97.  * powers of 2 (and 24), we just go ahead and enumerate them.
  98.  */
  99. #define max_value(depth)\
  100.   (depth >= 8 ? 255 : depth == 4 ? 15 : depth == 2 ? 3 : 1)
  101. #define mem_device(name, depth, procs)\
  102. {    sizeof(gx_device_memory),\
  103.     &procs,            /* differs */\
  104.     name,            /* differs */\
  105.     0, 0,            /* x and y extent (filled in) */\
  106.     72, 72,            /* density (makes initclip come out right) */\
  107.     no_margins,        /* margins */\
  108.        {    (depth >= 4 ? 3 : 1),    /* num_components */\
  109.         depth,\
  110.         max_value(depth),    /* max_gray */\
  111.         max_value(depth),    /* max_rgb */\
  112.         max_value(depth) + 1,    /* dither_gray */\
  113.         max_value(depth) + 1,    /* dither_color */\
  114.        },\
  115.     0,            /* not open yet */\
  116.     identity_matrix_body,    /* initial matrix (filled in) */\
  117.     0,            /* raster (filled in) */\
  118.     (byte *)0,        /* base (filled in) */\
  119.     (byte **)0,        /* line_ptrs (filled in by mem_open) */\
  120.     0,            /* invert (filled in for mono) */\
  121.     0, (byte *)0,        /* palette (filled in for color) */\
  122.     0            /* memory_procs */\
  123. }
  124.  
  125. /* Macro for casting gx_device argument */
  126. #define mdev ((gx_device_memory *)dev)
  127.  
  128. /*
  129.  * Macros for processing bitmaps in the largest possible chunks.
  130.  * Bits within a byte are always stored big-endian;
  131.  * bytes are likewise stored in left-to-right order, i.e., big-endian.
  132.  * Note that this is the format used for the source of copy_mono.
  133.  * It used to be the case that bytes were stored in the natural
  134.  * platform order, and the client had force them into big-endian order
  135.  * by calling gdev_mem_ensure_byte_order, but this no longer necessary.
  136.  *
  137.  * Note that we use type uint for register variables holding a chunk:
  138.  * for this reason, the chunk size cannot be larger than uint.
  139.  */
  140. /* Generic macros for chunk accessing. */
  141. #define cbytes(ct) size_of(ct)    /* sizeof may be unsigned */
  142. #  define chunk_bytes cbytes(chunk)
  143. /* The clog2_bytes macro assumes that ints are 2, 4, or 8 bytes in size. */
  144. #define clog2_bytes(ct) (size_of(ct) == 8 ? 3 : size_of(ct)>>1)
  145. #  define chunk_log2_bytes clog2_bytes(chunk)
  146. #define cbits(ct) (size_of(ct)*8)    /* sizeof may be unsigned */
  147. #  define chunk_bits cbits(chunk)
  148. #define clog2_bits(ct) (clog2_bytes(ct)+3)
  149. #  define chunk_log2_bits clog2_bits(chunk)
  150. #define cbit_mask(ct) (cbits(ct)-1)
  151. #  define chunk_bit_mask cbit_mask(chunk)
  152. #define calign_bytes(ct)\
  153.   (sizeof(ct) == 1 ? 1:\
  154.    sizeof(ct) == sizeof(short) ? arch_align_short_mod :\
  155.    sizeof(ct) == sizeof(int) ? arch_align_int_mod: arch_align_long_mod)
  156. #  define chunk_align_bytes calign_bytes(chunk)
  157. #define calign_bit_mask(ct) (calign_bytes(ct)*8-1)
  158. #  define chunk_align_bit_mask calign_bit_mask(chunk)
  159. /*
  160.  * The obvious definition for cmask is:
  161.  *    #define cmask(ct) ((ct)~(ct)0)
  162.  * but this doesn't work on the VAX/VMS compiler, which fails to truncate
  163.  * the value to 16 bits when ct is ushort.
  164.  * Instead, we have to generate the mask with no extra 1-bits.
  165.  * We can't do this in the obvious way:
  166.  *    #define cmask(ct) ((1 << (size_of(ct) * 8)) - 1)
  167.  * because some compilers won't allow a shift of the full type size.
  168.  * Instead, we have to do something really awkward:
  169.  */
  170. #define cmask(ct) ((ct) (((((ct)1 << (size_of(ct)*8-2)) - 1) << 2) + 3))
  171. #  define chunk_all_bits cmask(chunk)
  172. /*
  173.  * The obvious definition for chi_bits is:
  174.  *    #define chi_bits(ct,n) (cmask(ct)-(cmask(ct)>>(n)))
  175.  * but this doesn't work on the DEC/MIPS compilers.
  176.  * Instead, we have to restrict chi_bits to only working for values of n
  177.  * between 0 and cbits(ct)-1, and use
  178.  */
  179. #define chi_bits(ct,n) (ct)(~(ct)1 << (cbits(ct)-1 - (n)))
  180. #  define chunk_hi_bits(n) chi_bits(chunk,n)
  181.  
  182. /* Define whether this is a machine where chunks are long, */
  183. /* but the machine can't shift a long by its full width. */
  184. #define arch_cant_shift_full_chunk\
  185.   (arch_is_big_endian && !arch_ints_are_short && !arch_can_shift_full_long)
  186.  
  187. /*
  188.  * Macro for adding an offset to a pointer.
  189.  * This isn't just pointer arithmetic, because of the segmenting
  190.  * considerations discussed above.
  191.  * Note that this only works for byte * (or char *) pointers!
  192.  */
  193. #  define byte_ptr_add(base, offset)\
  194.      ((byte *)((byte huge *)(base) + (offset)))
  195.  
  196. /*
  197.  * Macros for scan line access.
  198.  * x_to_byte is different for each number of bits per pixel.
  199.  * Note that these macros depend on the definition of chunk:
  200.  * each procedure that uses the scanning macros should #define
  201.  * (not typedef) chunk as either uint or byte.
  202.  */
  203. #define scan_line_base(dev,y) (dev->line_ptrs[y])
  204. #define declare_scan_ptr(ptr)   declare_scan_ptr_as(ptr, chunk *)
  205. #define declare_scan_ptr_as(ptr,ptype)\
  206.     register ptype ptr; uint draster
  207. #define inc_chunk_ptr(ptr,delta)\
  208.     ptr = (chunk *)((byte *)ptr + (delta))
  209. #define setup_rect(ptr)   setup_rect_as(ptr, chunk *)
  210. #define setup_rect_as(ptr,ptype)\
  211.     draster = mdev->raster;\
  212.     ptr = (ptype)(scan_line_base(mdev, y) +\
  213.         (x_to_byte(x) & -chunk_align_bytes))
  214.  
  215. /* Define macros for setting up left- and right-end masks. */
  216. /* These are used for monobit operations, and for filling */
  217. /* with 2- and 4-bit-per-pixel patterns. */
  218.  
  219. /* Define the chunk size for monobit operations. */
  220. #if arch_is_big_endian
  221. #  define mono_chunk uint
  222. #  define set_mono_right_mask(var, w)\
  223.     var = ((w) == chunk_bits ? chunk_all_bits : chunk_hi_bits(w))
  224. /*
  225.  * We have to split following statement because of a bug in the Xenix C
  226.  * compiler (it produces a signed rather than an unsigned shift if we don't
  227.  * split).
  228.  */
  229. #  define set_mono_thin_mask(var, w, bit)\
  230.     set_mono_right_mask(var, w), var >>= (bit)
  231. /*
  232.  * We have to split the following statement in two because of a bug
  233.  * in the DEC VAX/VMS C compiler.
  234.  */
  235. #  define set_mono_left_mask(var, bit)\
  236.     var = chunk_all_bits, var >>= (bit)
  237. #else
  238. #  define mono_chunk ushort
  239. extern const mono_chunk gdev_mem_swapped_left_masks[17];
  240. #  define set_mono_right_mask(var, w)\
  241.     var = ~gdev_mem_swapped_left_masks[w]
  242. #  define set_mono_thin_mask(var, w, bit)\
  243.     var = ~gdev_mem_swapped_left_masks[(w) + (bit)] &\
  244.         gdev_mem_swapped_left_masks[bit]
  245. #  define set_mono_left_mask(var, bit)\
  246.     var = gdev_mem_swapped_left_masks[bit]
  247. #endif
  248.